﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KerriganDirection.DirectionServiceWCF;
using System.Diagnostics;
using KerriganDirection.Helpers;
using System.Windows.Controls;
using System.ComponentModel;


namespace KerriganDirection.SessionMgt
{
    public class SessionModule : INotifyPropertyChanged
    {
        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion

        #region Properties

        private MainModule _MainModuleVO;
        public MainModule MainModuleVO
        {
            get
            {
                return this._MainModuleVO;
            }
            set
            {
                this._MainModuleVO = value;
                OnPropertyChanged("MainWindowVO");
            }
        }

        private SessionDC _SessionVO;
        public SessionDC SessionVO
        {
            get
            {
                return this._SessionVO;
            }
            set
            {
                this._SessionVO = value;
                OnPropertyChanged("SessionVO");
            }
        }

        private ucAddSession _UcAddSessionVO;
        public ucAddSession UcAddSessionVO
        {
            get
            {
                return this._UcAddSessionVO;
            }
            set
            {
                this._UcAddSessionVO = value;
                OnPropertyChanged("UcAddSessionVO");
            }
        }

        //private ucEditSession _UcEditSessionVO;
        //public ucEditSession UcEditSessionVO
        //{
        //    get
        //    {
        //        return this._UcEditSessionVO;
        //    }
        //    set
        //    {
        //        this._UcEditSessionVO = value;
        //        OnPropertyChanged("UcEditSessionVO");
        //    }
        //}

        private ucSessionsList _UcSessionListVO;
        public ucSessionsList UcSessionListVO
        {
            get
            {
                return this._UcSessionListVO;
            }
            set
            {
                this._UcSessionListVO = value;
                OnPropertyChanged("UcSessionListVO");
            }
        }

        private ucSessionOptions _UsSessionOptionsVO;
        public ucSessionOptions UcSessionOptionsVO
        {
            get
            {
                return this._UsSessionOptionsVO;
            }
            set
            {
                this._UsSessionOptionsVO = value;
                OnPropertyChanged("UsSessionOptionsVO");
            }
        }

        private vwGetSession[] _ListSessionsVOToDisplay;
        public vwGetSession[] ListSessionsVOToDisplay
        {
            get
            {
                return this._ListSessionsVOToDisplay;
            }
            set
            {
                this._ListSessionsVOToDisplay = value;
                OnPropertyChanged("ListSessionsVOToDisplay");
            }
        }

        //private SessionDC[] _ListSessionsVO;
        //public SessionDC[] ListSessionsVO
        //{
        //    get
        //    {
        //        return this._ListSessionsVO;
        //    }
        //    set
        //    {
        //        this._ListSessionsVO = value;
        //        OnPropertyChanged("ListSessionsVO");
        //    }
        //}

        private FormationDC[] _ListFormationsVO;
        public FormationDC[] ListFormationsVO
        {
            get
            {
                return this._ListFormationsVO;
            }
            set
            {
                this._ListFormationsVO = value;
                OnPropertyChanged("ListFormationsVO");
            }
        }

        private LevelSessionDC[] _ListLevelsSessionVO;
        public LevelSessionDC[] ListLevelsSessionVO
        {
            get
            {
                return this._ListLevelsSessionVO;
            }
            set
            {
                this._ListLevelsSessionVO = value;
                OnPropertyChanged("ListLevelsSessionVO");
            }
        }

        private FormationCategoryDC[] _ListFormationCategoryVO;
        public FormationCategoryDC[] ListFormationCategoryVO
        {
            get
            {
                return this._ListFormationCategoryVO;
            }
            set
            {
                this._ListFormationCategoryVO = value;
                OnPropertyChanged("ListFormationCategoryVO");
            }
        }

        private VehicleDC[] _ListVehicleVO;
        public VehicleDC[] ListVehicleVO
        {
            get
            {
                return this._ListVehicleVO;
            }
            set
            {
                this._ListVehicleVO = value;
                OnPropertyChanged("ListVehicleVO");
            }
        }

        private InstructorDC[] _ListInstructorVO;
        public InstructorDC[] ListInstructorVO
        {
            get
            {
                return this._ListInstructorVO;
            }
            set
            {
                this._ListInstructorVO = value;
                OnPropertyChanged("ListInstructorVO");
            }
        }

        private AgencyDC[] _ListAgencyVO;
        public AgencyDC[] ListAgencyVO
        {
            get
            {
                return this._ListAgencyVO;
            }
            set
            {
                this._ListAgencyVO = value;
                OnPropertyChanged("ListAgencyVO");
            }
        }

        private InstructorAvailabilityDC[] _ListInstructorAvailabilityVO;
        public InstructorAvailabilityDC[] ListInstructorAvailabilityVO
        {
            get
            {
                return this._ListInstructorAvailabilityVO;
            }
            set
            {
                this._ListInstructorAvailabilityVO = value;
                OnPropertyChanged("ListInstructorAvailabilityVO");
            }
        }

        private GetVehicleAvailableByDate_Result[] _ListVehicleAvailableVO;
        public GetVehicleAvailableByDate_Result[] ListVehicleAvailableVO
        {
            get
            {
                return this._ListVehicleAvailableVO;
            }
            set
            {
                this._ListVehicleAvailableVO = value;
                OnPropertyChanged("ListVehicleAvailableVO");
            }
        }

        #endregion

        #region Contructors

        public SessionModule()
        {
        }

        #endregion

        #region Methods

        public void InitModule()
        {
            try
            {
                this.MainModuleVO = (App.Current.Windows[0] as MainWindow).MainModuleVO;

                RefreshList();

                this.UcSessionListVO = new ucSessionsList();
                this.UcSessionOptionsVO = new ucSessionOptions();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void RefreshList()
        {
            //Initialization List
            this.GetListSessionToDisplay();
            this.GetListFormations();
            this.GetListLevelSession();
            this.GetListFormationCategory();
            this.GetListAgency();
            this.GetListInstructor();
            this.GetListInstructorAvailable();
            this.GetListVehicleAvailable();
        }

        public void SetSession()
        {
            try
            {
                ServiceProxy.Service.SetSession(this.SessionVO);
                this.SessionVO = new SessionDC();
                this.MainModuleVO.GoToLitSession();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void DeleteSession(int id)
        {
            try
            {
                ServiceProxy.Service.DeleteSession(id);
                this.SessionVO = null;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetSession(int IdSession)
        {
            try
            {
                this.SessionVO = null;
                this.SessionVO = ServiceProxy.Service.GetSession(IdSession);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListSessionToDisplay()
        {
            try
            {
                this.ListSessionsVOToDisplay = ServiceProxy.Service.GetListSessionsToDisplay();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListFormations()
        {
            try
            {
                this.ListFormationsVO = ServiceProxy.Service.GetListFormations();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListLevelSession()
        {
            try
            {
                this.ListLevelsSessionVO = ServiceProxy.Service.GetListLevelSessions();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListFormationCategory()
        {
            try
            {
                this.ListFormationCategoryVO = ServiceProxy.Service.GetFormationCategories();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListAgency()
        {
            try
            {
                this.ListAgencyVO = ServiceProxy.Service.GetListAgency();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListInstructor()
        {
            try
            {
                this.ListInstructorVO = ServiceProxy.Service.GetListInstructor();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListVehicle()
        {
            try
            {
                this.ListVehicleVO = ServiceProxy.Service.GetListVehicle();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListVehicleAvailable()
        {
            try
            {
                if (SessionVO != null)
                    if (SessionVO.Date != null && SessionVO.EndDate != null)
                        this.ListVehicleAvailableVO = ServiceProxy.Service.GetVehicleAvailableByDate(this.SessionVO.Date, this.SessionVO.EndDate);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public void GetListInstructorAvailable()
        {
            try
            {
                this.ListInstructorAvailabilityVO = ServiceProxy.Service.GetListInstructorAvailable();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        public List<GetInstructorAvaibleBySession_Result> GetListInstructorBySession()
        {
            try
            {
                return ServiceProxy.Service.GetListInstructorBySession(this.SessionVO).ToList();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            return null;
        }

        #endregion
    }
}
